// pages/shop_send/send.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendList: '',
    order_id: '',
    expressTxt: '填写快递单号',
    expressPro: '',
    popVal: '',
    showMyModel: false,
    listExpressBol: false,
    listIndex: -1
  },
  init: function() {

  },
  getExpressList: function(callback) {
    let shopData = wx.getStorageSync('shop_data_shop');
    let shop_code = wx.getStorageSync('shopInfo').shop_code; // 商店id
    let self = this;
    api.ajax(service.url('getExpressList'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key,
      shop_code: shop_code,
    }, function(res) {
      console.log("获取快递：" + JSON.stringify(res));
      if (res.code) {
        api.toast(res.message || '数据出现错误');
      } else {
        callback(res)
      }
    });
  },
  //--手动添加其他快递
  btnExpressSend: function() {
    this.setData({
      listExpressBol: false,
      showMyModel: true,
      expressTxt: '请填写快递名称',
      expressPro: '请填写快递名称',
      popVal: ''
    });
  },
  //--填写快递单号
  listExpressNumber: function(et) {
    this.setData({
      listExpressBol: true,
      showMyModel: true,
      listIndex: +et.currentTarget.dataset.index,
      expressTxt: '请填写快递单号',
      expressPro: "请填写" + this.data.sendList[+et.currentTarget.dataset.index].express_name + "的快递单号",
      popVal: ''
    });
  },
  //--获取弹窗输入信息
  inputValue: function(et) {
    this.setData({
      popVal: et.detail.value
    });
  },
  //--关闭弹窗
  colsePop: function() {
    this.setData({
      showMyModel: false
    });
  },
  //--弹窗确定
  surePop: function() {
    let self = this;
    if (this.data.listExpressBol) {
      //--填写快递单号
      //--判断是否仅为英文和数字
      if (!/^[0-9A-Za-z]+$/.test(this.data.popVal)) {
        api.toast("请填写正确的快递单号！");
        return;
      };
      let pageParam = {
        shop_code: wx.getStorageSync('shopInfo').shop_code,
        order_id: this.data.order_id,
        express_id: this.data.sendList[this.data.listIndex].express_id,
        express_name: this.data.sendList[this.data.listIndex].express_name,
        express_sn: this.data.popVal
      };
      setTimeout(function() {
        self.setData({
          showMyModel: false
        });
      }, 500);

      if (this.data.flowEdit){
        api.ajax(service.url('editExpressSn'), {
          express_id: pageParam.express_id,
          express_sn: pageParam.express_sn,
          order_id: pageParam.order_id
        }, function (res) {
          console.log(res)
        })

        wx.navigateBack({
          success: function(){
            getCurrentPages()[getCurrentPages().length - 1].init()
          }
        })
      }else{
        wx.navigateTo({
          url: '../shop_send/detail/detail?pageParam=' + JSON.stringify(pageParam)
        });
      }
    } else {
      //--添加快递名称
      let shopData = wx.getStorageSync('shop_data');
      let shop_code = wx.getStorageSync('shopInfo').shop_code; // 商店id
      if (self.data.popVal != '' && self.data.popVal != null) {
        //--判断是否存在特殊字符
        /*if(app.hasSpecialChar(express_name, '请正确填写快递公司名称')) {
            return false
        };*/
        api.ajax(service.url('setExpress'), {
          unionid: shopData.shop_unionid,
          token: shopData.token,
          token_key: shopData.token_key,
          shop_code: shop_code,
          express_name: self.data.popVal,
          express_code: ''
        }, function(res) {
          console.log("填写快递：" + JSON.stringify(res));
          if (res.code == 0) {
            self.setData({
              showMyModel: false
            });
            api.toast('添加快递列表成功!');
            self.getExpressList(function(res) {
              self.setData({
                sendList: res.data
              });
            });
          } else {
            api.toast(res.message || '数据出现错误！');
          }
        });
      } else {
        api.toast('快递公司名称不能为空');
        return;
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    this.setData({
      order_id: options.order_id
    });
    this.getExpressList(function(res) {
      self.setData({
        sendList: res.data
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
